import { onUnmounted, ref } from 'vue'
import {useIntersectionObserver,useIntervalFn} from '@vueuse/core'
// 懒加载Api
export function useLazyData(apiFn:()=>void){
  const target = ref(null)
  const {stop} = useIntersectionObserver(target,([{isIntersecting}])=>{
    if(isIntersecting){
      apiFn()  //形参 请求
      stop()
    }
  },
  {
    threshold:0
  })
  return target
}
// 倒计时api
export function useCountDown(count:number = 60){
  const time = ref(0)
const {pause,resume} = useIntervalFn(()=>{
  time.value--
  if (time.value === 0) {
    // 清除定时器
    pause()
  }
},1000,{
  // 不立即触发
  immediate:false
})
// 注销前清除定时器
onUnmounted(()=>{
  pause()
})
// 倒计时
const start  = ()=>{
  time.value = count
  resume()
}
return {
  start,
  time
}
}

